﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class QuanLyNhomHangHoa : UserControl
    {
        public QuanLyNhomHangHoa()
        {
            InitializeComponent();
        }

        private void QuanLyNhomHangHoa_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            dgvNhomHangHoa.Rows.Clear();

            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var NhomHangHoas = from ig in dc.NhomHangHoas where ig.deleted == false select ig;
            int count = 1;
            foreach (_DB.NhomHangHoa ig in NhomHangHoas)
            {
                DataGridViewRow row = new DataGridViewRow();
                DataGridViewCell c;

                c = new DataGridViewTextBoxCell();
                c.Value = count.ToString();
                row.Cells.Add(c);

                c = new DataGridViewTextBoxCell();
                c.Value = ig.TenNhomHangHoa;
                row.Cells.Add(c);

                c = new DataGridViewTextBoxCell();
                c.Value = ig.HangHoas.Count;
                row.Cells.Add(c);

                row.Tag = ig.NhomHangHoa_ID;
                dgvNhomHangHoa.Rows.Add(row);
                count++;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            OtherForms.ThemSuaNhomHangHoaForm f = new OtherForms.ThemSuaNhomHangHoaForm();
            f.ShowDialog();
            loadData();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo) != DialogResult.Yes)
                return;

            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            foreach (DataGridViewRow row in dgvNhomHangHoa.SelectedRows)
            {
                int ID = (int)row.Tag;

                _DB.NhomHangHoa igr = (from ig in dc.NhomHangHoas
                                     where (ig.NhomHangHoa_ID == ID) 
                                     select ig).Single();

                igr.deleted = true;
            }

            dc.SubmitChanges();
            loadData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgvNhomHangHoa.SelectedRows.Count == 1)
            {
                int ID = (int)dgvNhomHangHoa.SelectedRows[0].Tag;
                OtherForms.ThemSuaNhomHangHoaForm f = new OtherForms.ThemSuaNhomHangHoaForm(ID);
                f.ShowDialog();
                loadData();
            }
        }

    }
}
